package com.nbcio.modules.erp.settleaccount.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nbcio.modules.erp.base.entity.ErpCustomer;
import com.nbcio.modules.erp.base.service.IErpCustomerService;
import com.nbcio.modules.erp.cart.entity.SysUser;
import com.nbcio.modules.erp.cart.service.ISysUserServiceNew;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import com.nbcio.modules.erp.settleaccount.entity.SettlementAccount;
import com.nbcio.modules.erp.settleaccount.service.ISettlementAccountService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 结算账户
 * @Author: nbacheng
 * @Date:   2024-04-24
 * @Version: V1.0
 */
@Api(tags="结算账户")
@RestController
@RequestMapping("/bd/settlementAccount")
@Slf4j
public class SettlementAccountController extends JeecgController<SettlementAccount, ISettlementAccountService> {
	@Autowired
	private ISettlementAccountService settlementAccountService;

	@Autowired
	private ISysUserServiceNew sysUserService;

	@Autowired
	private IErpCustomerService erpCustomerService;
	
	/**
	 * 分页列表查询
	 *
	 * @param settlementAccount
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "结算账户-分页列表查询")
	@ApiOperation(value="结算账户-分页列表查询", notes="结算账户-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SettlementAccount settlementAccount,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SettlementAccount> queryWrapper = QueryGenerator.initQueryWrapper(settlementAccount, req.getParameterMap());
		Page<SettlementAccount> page = new Page<SettlementAccount>(pageNo, pageSize);
		IPage<SettlementAccount> pageList = settlementAccountService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param settlementAccount
	 * @return
	 */
	@AutoLog(value = "结算账户-添加")
	@ApiOperation(value="结算账户-添加", notes="结算账户-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SettlementAccount settlementAccount) {
		settlementAccountService.save(settlementAccount);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param settlementAccount
	 * @return
	 */
	@AutoLog(value = "结算账户-编辑")
	@ApiOperation(value="结算账户-编辑", notes="结算账户-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SettlementAccount settlementAccount) {
		settlementAccountService.updateById(settlementAccount);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "结算账户-通过id删除")
	@ApiOperation(value="结算账户-通过id删除", notes="结算账户-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		settlementAccountService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "结算账户-批量删除")
	@ApiOperation(value="结算账户-批量删除", notes="结算账户-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.settlementAccountService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "结算账户-通过id查询")
	@ApiOperation(value="结算账户-通过id查询", notes="结算账户-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SettlementAccount settlementAccount = settlementAccountService.getById(id);
		if(settlementAccount==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(settlementAccount);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param settlementAccount
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SettlementAccount settlementAccount) {
        return super.exportXls(request, settlementAccount, SettlementAccount.class, "结算账户");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SettlementAccount.class);
    }


	 /**
	  * 通过客户id和支付方式查询
	  *
	  * @param map
	  * @return
	  */
	 @AutoLog(value = "结算账户-客户id查询")
	 @ApiOperation(value="结算账户-客户id查询", notes="结算账户-客户id查询")
	 @PostMapping(value = "/queryByCustomerAndType")
	 public Result<?> queryByCustomerAndType(@RequestBody Map<String,Object> map) {

		 String user_id = map.get("user_id").toString();

		 LambdaQueryWrapper<SysUser> userWrapper = new LambdaQueryWrapper<>();
		 userWrapper.eq(SysUser::getId, user_id);
		 SysUser user = sysUserService.getOne(userWrapper);
		 if (user==null) {
			 return Result.error("未找到对应用户数据");
		 }

		 String phone = user.getPhone();
		 LambdaQueryWrapper<ErpCustomer> query = new LambdaQueryWrapper<>();
		 query.eq(ErpCustomer::getPhone,phone );
		 ErpCustomer erpCustomer = erpCustomerService.getOne(query);
		 if (erpCustomer==null){
			 return Result.error("未找到对应客户数据");
		 }

		 String customerId = erpCustomer.getId();

		 LambdaQueryWrapper<SettlementAccount> wrapper = new LambdaQueryWrapper<>();
		 wrapper.eq(SettlementAccount::getCustomerId, customerId)
				 .eq(SettlementAccount::getStatus, "enable");

		 List<SettlementAccount> settlementAccountList = settlementAccountService.list(wrapper);
		 if (CollectionUtil.isEmpty(settlementAccountList)){
			 return Result.OK(new ArrayList<>());
		 }

		 return Result.OK(settlementAccountList);
	 }

}
